import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/game_tab.dart';
import 'package:flutter_syt/domain/games/SportList.dart';
import 'package:flutter_syt/domain/games/event_schedule.dart';
import 'package:flutter_syt/domain/games/game_live_list.dart';
import 'package:flutter_syt/domain/games/game_utils/game_utils_controller.dart';
import 'package:flutter_syt/domain/games/game_video_list.dart';
import 'package:flutter_syt/domain/games/subscription_page.dart';
import 'package:flutter_syt/domain/games/venue_page.dart';
import 'package:flutter_syt/domain/games/wardList.dart';
import 'package:flutter_syt/domain/home/share_api.dart';
import 'package:flutter_syt/domain/widgets/stupid_tabs.dart';
import 'package:flutter_syt/domain/widgets/stupid_title_bar.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/normal_grid.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:flutter_syt/widgets/view_state_widget.dart';

class GamesPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _GamesPageState();
}

class _GamesPageState extends State<GamesPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  bool isLoading = true;
  String? error;
  List<GameTabData> tabs = [];
  @override
  void initState() {
    getData();
    super.initState();
  }

  getData() async {
    setState(() {
      isLoading = true;
    });
    try {
      tabs = await API.shared.game.tabs();
      _tabController = TabController(length: tabs.length, vsync: this);
      error = null;
    } catch (e) {
      error = "$e";
    }
    setState(() {
      isLoading = false;
    });
  }

  Widget loading() {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            "assets/icons/empty.png",
            width: 80,
            height: 80,
          ),
          Container(
            height: 12,
          ),
          Text(
            S.of(context).loading,
            style:
                TextStyle(color: Colors.black.withOpacity(0.4), fontSize: 14),
          ),
          Container(
            height: 20,
          ),
        ],
      ),
    );
  }

  Widget body() {
    var normalTabs = tabs
        .map((e) => ScrollerTabEntity.create(e.func, e.name, 1, e.func))
        .toList();
    var dev = Container(
      child: Center(
        child: Text(S().standy_by),
      ),
    );
    var list = tabs.map((e) {
      if (e.func == "5") {
        return VenueList();
      } else if (e.func == "4") {
        return SportsListContent();
      } else if (e.func == '2') {
        return EventSchedule();
        // return Container();
      } else if (e.func == '3') {
        return WardListHome();
      } else if (e.func == "1") {
        return SubscriptionPage();
      } else if (e.func == '6') {
        // 直播

        var page = GameLiveListPage();
        page.needBar = false;
        return page;
      } else if (e.func == '7') {
        // 点播
        var page = GameVideoListPage();
        page.needBar = false;
        return page;
      }
      return dev;
    }).toList();
    return Stack(
      children: [
        Column(
          children: [
            NormalTabs(
              normalTabs,
              _tabController,
              textColor: Colors.black,
              // alignment: Alignment.centerLeft,
            ),
            Container(
              color: Colors.black.withOpacity(0.1),
              height: 0.5,
            ),
            Expanded(
              child: TabBarView(
                controller: _tabController,
                children: list,
              ),
            ),
          ],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget content;
    if (isLoading) {
      content = loading();
    } else if (error != null) {
      content = ViewStateErrorWidget(
        onPressed: ClickUtils.debounce(() {
          getData();
        }),
      );
    } else {
      content = body();
    }
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            GestureDetector(
              child: Padding(
                padding: const EdgeInsets.only(bottom: 0),
                child: ColorsTitleBar(
                  weatherEntity,
                  title: S.current.competitions,
                  type: BarType.SEARCH,
                  isShowWeather: false,
                  isCompetitions: true,
                ),
              ),
              onDoubleTap: () {
                var controller = GameUtilsController
                    .managerCont[tabs[_tabController.index].func];
                if (controller is NormalTableViewController) {
                  controller.animateTo(0);
                } else if (controller is NormalGridController) {
                  controller.animateTo(0);
                } else if (controller is EventTableViewController) {
                  controller.animateTo(0);
                }
              },
              behavior: HitTestBehavior.opaque,
            ),
            Expanded(
              child: content,
            ),
          ],
        ),
      ),
    );
  }
}
